package com.example.hadoop.mapreduce.flowsort;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Brian
 * @Date: 2020/04/22/20:34
 * @Description: 13480253104     上行流量：180   下行流量：180   总流量：360
 * 13502468823     上行流量：7335  下行流量：110349        总流量：117684
 * 13560436666     上行流量：1116  下行流量：954   总流量：2070
 * 13560439658     上行流量：2034  下行流量：5892  总流量：7926
 */
public class FlowSort {

    /**
     * KEY VALUE 是对应Mapper输出的key value类型
     */

    static class FlowCountMapper extends Mapper<LongWritable, Text, FlowSortBean, Text> {

        FlowSortBean bean = new FlowSortBean();
        Text v = new Text();

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //获取一行日志内容
            String record = value.toString();
            //将日志记录进行切割
            String[] fields = record.split("\t");
            // 截取手机号
            String phone = fields[0];
            //截取上行流量和下行流量
            String upFlowDesc = fields[1];
            String dFlowDesc = fields[2];
            long upFlow = Long.parseLong(upFlowDesc.split("：")[1]);
            long dFlow = Long.parseLong(dFlowDesc.split("：")[1]);
            bean.set(upFlow, dFlow);
            v.set(phone);
            //context.write 的时候会将key value序列化（即写到文件中），在reduce task中再反序列化回来
            context.write(bean, v);
        }
    }

    //<bean1,12345643223>
    //<bean2,13243334424>
    static class FlowCountReducer extends Reducer<FlowSortBean, Text, Text, FlowSortBean> {
        @Override
        protected void reduce(FlowSortBean key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            context.write(values.iterator().next(), key);
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        if (args == null || args.length < 2) {
            args[0] = "/wordcount/flowinput";
            args[1] = "/wordcount/flowoutput";
        }
        //加载默认配置
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        //当程序运行时，通过类加载器获取类的所在路径
        job.setJarByClass(FlowSort.class);
        //配置mapper和reducer类
        job.setMapperClass(FlowCountMapper.class);
        job.setReducerClass(FlowCountReducer.class);

        //配置map的输出类型
        job.setMapOutputKeyClass(FlowSortBean.class);
        job.setMapOutputValueClass(Text.class);

        //配置最终输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputKeyClass(FlowSortBean.class);

        Path outputPath = new Path(args[1]);
        FileSystem fileSystem = FileSystem.get(new Configuration());
        if (fileSystem.exists(outputPath)) {
            fileSystem.delete(outputPath, true);
        }
        //设置数据源路径和输入路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, outputPath);

        //将Job提交给Yarn
        boolean successfully = job.waitForCompletion(true);
        System.out.println("Successfully? --> " + successfully);
        System.exit(successfully ? 0 : 1);
    }
}
